// pages/user/login/index/index.js
import Notify from "@vant/weapp/notify/notify";
import Toast from "@vant/weapp/toast/toast";
import {verifyInput, verifyPhone, navigatePage} from "../../../../utils/Util";
import User from "../../../../service/User";
import Storage from "../../../../utils/Storage";
import API from "../../../../service/API"
import {getResult} from "../../../../service/Questionnaire";
import Revisit from "../../../../service/Revisit";
import {showModal} from "../../../../service/Wx";
Page({

    /**
     * 页面的初始数据
     */
    data: {
        formData: {
            type: "",
            tel: "",
            code: null
        },
        btnLoading: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let userType = Storage.getStorageSync("userType");
        userType ? this.setData({"formData.type": parseInt(userType)}) : null;
    },

    /*
    * input输入
    * */
    onInputChange: function (e) {
        let name = e.currentTarget.dataset.name;
        let val = e.detail.type || e.detail;
        this.setData({["formData." + name]: val});
    },

    /*
    * 内容提交
    * */
    onSubmit: async function () {
        try {
            const that = this;
            let formData = that.data.formData;
            let type = formData.type;
            let params = {type: type}, api;
            if (!verifyInput(formData.type)) {
                Notify({type: "warning", message: "请选择客户类型"});
                return false
            }
            switch (parseInt(type)) {
                case 1: //个人
                    if (!verifyInput(formData.tel)) {
                        Notify({type: "warning", message: "手机号不能为空"});
                        return false
                    }
                    if (!verifyPhone(formData.tel)) {
                        Notify({type: "warning", message: "手机号格式不正确"});
                        return false
                    }
                    if (!verifyInput(formData.code)) {
                        Notify({type: "warning", message: "短信验证码不能为空"});
                        return false
                    }
                    api = API.login;
                    params.tel = formData.tel;
                    params.code = formData.code;
                    break
                case 2: //机构
                    if (!verifyInput(formData.paper_num)) {
                        Notify({type: "warning", message: "机构编号不能为空"});
                        return false
                    }
                    if (!verifyInput(formData.password)) {
                        Notify({type: "warning", message: "密码不能为空"});
                        return false
                    }
                    api = API.jgLogin;
                    params.paper_num = formData.paper_num;
                    params.password = formData.password;
                    break
                case 3: //产品
                    if (!verifyInput(formData.record_num)) {
                        Notify({type: "warning", message: "备案编号不能为空"});
                        return false
                    }
                    if (!verifyInput(formData.password)) {
                        Notify({type: "warning", message: "密码不能为空"});
                        return false
                    }
                    api = API.cpLogin;
                    params.record_num = formData.record_num;
                    params.password = formData.password;
                    break
                default:
                    break
            }
            if (that.data.btnLoading) {
                return false
            } else {
                that.setData({btnLoading: true});
                let user = await User.login(api, params);
                Storage.setStorageSync("userType", parseInt(type));
                Storage.setStorageSync("token", {
                    value: user.access_token, //token
                    times: parseInt(new Date().getTime() / 1000)  //生成时间
                });
                let userInfo = await User.getInfo();
                Storage.setStorageSync("userInfo", userInfo);
                //产品客户不需要填写问卷调查以及回访问卷
                if (parseInt(type) === 3 || userInfo.reg_type !== 1) { //产品用户
                    Toast({
                        type: "success",
                        mask: true,
                        message: "登录成功",
                        onClose: async () => {
                            await that._checkStatus();
                        }
                    });
                } else {
                    let queResult = await getResult(); //获取问卷调查结果
                    if (queResult.level && queResult.score) { //已经完成调查
                        Toast({
                            type: "success",
                            mask: true,
                            message: "登录成功",
                            onClose: async () => {
                                await that._checkStatus();
                            }
                        });
                    } else {
                        Toast({
                            type: "success",
                            mask: true,
                            message: "登录成功",
                            onClose: () => {
                                wx.reLaunch({url: "/packageQuestionnaire/pages/list/list?from=register"});
                            }
                        });
                    }
                }
            }
        } catch (e) {
            e.errCode ? Notify({type: "warning", message: e.errMsg}) : console.error(e);
        } finally {
            this.setData({btnLoading: false});
        }
    },

    /*
    * 获取回访调查状态
    * */
    _checkStatus: async function () {
        try {
            let isValidate = await Revisit.checkComplete();
            if (isValidate) return false;
            let toPage = Storage.getStorageSync("token.page");
            navigatePage(toPage, true);
            const eventChannel = this.getOpenerEventChannel(); //像上一个页面返回数据
            eventChannel && eventChannel.emit ? eventChannel.emit("acceptDataFromOpenedPage", {login: true}) : null;
            return isValidate
        } catch (e) {
            console.error(e)
        }
    }
})